import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {


        //m1();


        //m2();


        // m3();


        //m4();

        // 修改
        m5();

    }
    private static void m5(){
        try{
            Class.forName("com.mysql.cj,jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/mydb";
            Connection connection

        }
        catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }



    private static void m4() {

        try {

            Class.forName("com.mysql.cj.jdbc.Driver");


            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            Connection connection = DriverManager.getConnection(url, "root", "hhpp");
           if (connection != null) {
                System.out.println("连接成功");
            }

            String sql = "delete from sys_user where id = ? ";

            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setObject(1,"3");

            int result = preparedStatement.executeUpdate();

            if (result > 0) {
                System.out.println("删除成功");
            }


            preparedStatement.close();
            connection.close();


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private static void m3() {

        try {


            Scanner sc = new Scanner(System.in);
            System.out.print("请输入要新增的姓名：");
            String name = sc.next();
            System.out.print("请输入要新增的密码：");
            String pass = sc.next();

            Class.forName("com.mysql.cj.jdbc.Driver");


            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            Connection connection = DriverManager.getConnection(url, "root", "hhpp");
            if (connection != null) {
               System.out.println("连接成功");
           }
            String sql = "insert into sys_user(name,pass) values (?,?) ";


            PreparedStatement preparedStatement = connection.prepareStatement(sql);


            preparedStatement.setObject(1,name);
            preparedStatement.setObject(2,pass);

            int result = preparedStatement.executeUpdate();


            if (result > 0) {
                System.out.println("新增成功");
            }


            preparedStatement.close();
            connection.close();


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private static void m2() {

        try {


            Scanner sc = new Scanner(System.in);
            System.out.print("请输入你要查询的姓名：");
            String word = sc.next();

            Class.forName("com.mysql.cj.jdbc.Driver");


            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            Connection connection = DriverManager.getConnection(url, "root", "hhpp");
            if (connection != null) {
                System.out.println("连接成功");
           }

            String sql = "select * from sys_user where name like ? ";

            PreparedStatement preparedStatement = connection.prepareStatement(sql);


            preparedStatement.setObject(1,"%"+word+"%");//setObject给参数位置多种类型不出错

            ResultSet resultSet = preparedStatement.executeQuery();

            while (resultSet.next()){
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String pass = resultSet.getString(3);
                System.out.println(id+"-----"+name+"------"+pass);
            }
            resultSet.close();
            preparedStatement.close();
            connection.close();


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private static void m1() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            Connection connection = DriverManager.getConnection(url, "root", "hhpp");
           if (connection != null) {
               System.out.println("连接成功");
           }

            String sql = "select * from sys_user";


            PreparedStatement preparedStatement = connection.prepareStatement(sql);



            ResultSet resultSet = preparedStatement.executeQuery();


            while (resultSet.next()){
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String pass = resultSet.getString(3);
                System.out.println(id+"-----"+name+"------"+pass);
            }


            resultSet.close();
            preparedStatement.close();
            connection.close();


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}